<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@page import="org.jawa.Constants,org.jawa.util.ParamUtils"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.Locale"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.StringTokenizer"%>
<%! // Global vars

boolean testConnection(Map<String,String> errors,String defaultFrom,String host,String username,String password) {
    boolean success = true;
    
    return success;
}
Map<String,String> validatorMail(String defaultFrom,String host,String username,String password){
	Map<String,String> errors = new HashMap<String,String>();
	if(defaultFrom==null || defaultFrom.trim().equals("")){
		errors.put("from","");
	}
	if(host==null || host.trim().equals("")){
		errors.put("host","");
	}
	if(username==null || username.trim().equals("")){
		errors.put("username","");
	}
	if(password==null || password.trim().equals("")){
		errors.put("password","");
	}
	return errors;
}
%>
<%
    // Get parameters

	String defaultFrom = ParamUtils.getParameter(request,"defaultFrom");
    String host = ParamUtils.getParameter(request,"host");
    String username = ParamUtils.getParameter(request,"username",true);
    String password = ParamUtils.getParameter(request,"password",true);
    
    boolean doContinue = request.getParameter("continue") != null;

    // handle a mode redirect
    Map<String,String> errors = new HashMap<String,String>();
    if (doContinue) {
    	errors=validatorMail(defaultFrom, host, username, password);
    	 if (errors.size() == 0) {
	    	 Constants.setXMLProperty("mail.default.from", defaultFrom);
	         Constants.setXMLProperty("mail.host", host);
	         Constants.setXMLProperty("mail.username", username);
	         Constants.setXMLProperty("mail.password", password);
	         if (testConnection(errors,defaultFrom,host,username,password)) {
	             // Success, move on
	             response.sendRedirect("setup-admin-settings.jsp");
	             return;
	         }
    	 }
    }
    
    if (!doContinue) {
        // reset values of jdbc driver from props file
        try{
        	defaultFrom = Constants.getXMLProperty("mail.default.from");
        	host = Constants.getXMLProperty("mail.host");
        	username=  Constants.getXMLProperty("mail.username");
        	password =  Constants.getXMLProperty("mail.password");
        }catch(Exception e){}
    }
%>

<html>
<head>
<title><fmt:message key="setup.mail.settings.title" /></title>
<meta name="currentStep" content="4"/>
</head>
<body>
	<h1>
       <fmt:message key="setup.mail.settings.title" />
	</h1>
	<p>
	    <fmt:message key="setup.mail.settings.info"/>
	</p>

	<%  if (!errors.isEmpty() && doContinue) { %>
    <div class="error">
	    <table cellpadding="0" cellspacing="0" border="0">
		    <tbody>
		        <tr>
		            <td class="jawa-icon-label">
		
		            <% if (errors.get("from") != null) { %>
		                <fmt:message key="setup.mail.defaulfrom.error" />
		            <% } else if (errors.get("host") != null) { %>
		                <fmt:message key="setup.mail.host.error" />
		            <% } else if (errors.get("username") != null) { %>
		                <fmt:message key="setup.mail.username.error" />
		            <% } else if (errors.get("password") != null) { %>
		                <fmt:message key="setup.mail.password.error" />
		            <% } %>
		            </td>
		        </tr>
		    </tbody>
	    </table>
    </div>

	<%  }
	%>
	<!-- BEGIN jawa-contentBox -->
	<div class="jawa-contentBox">
			<form action="setup-mail-settings.jsp" name="sform" method="post">
			<table cellpadding="3" cellspacing="2" border="0">
				<tr valign="top">
				    <td nowrap align="right">
				        <fmt:message key="setup.mail.default.from" />
				    </td>
				    <td>
				        <input type="text" name="defaultFrom" value="<%= ((defaultFrom != null) ? defaultFrom : "") %>"  id="loc01" /> 
				    </td>
					<td>
						<span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.mail.default.from" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
							        <%  if (errors.get("defaultFrom") != null) { %>
							            <span class="jawa-error-text">
							            <%= errors.get("defaultFrom") %>
							            </span>
							        <%  } %>
					</td>
				</tr>
				<tr valign="top">
				    <td nowrap align="right">
				        <fmt:message key="setup.mail.host" />
				    </td>
				    <td>
				        <input type="text" name="host" value="<%= ((host != null) ? host : "") %>"  id="loc01" /> 
				    </td>
					<td>
						<span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.mail.host" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
							        <%  if (errors.get("host") != null) { %>
							            <span class="jawa-error-text">
							            <%= errors.get("host") %>
							            </span>
							        <%  } %>
					</td>
				</tr>
				<tr valign="top">
				    <td nowrap align="right">
				        <fmt:message key="setup.mail.username" />
				    </td>
				    <td>
				        <input type="text" name="username" value="<%= ((username != null) ? username : "") %>"  id="loc01" /> 
				    </td>
					<td>
						<span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.mail.username" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
							        <%  if (errors.get("username") != null) { %>
							            <span class="jawa-error-text">
							            <%= errors.get("username") %>
							            </span>
							        <%  } %>
					</td>
				</tr>
				<tr valign="top">
				    <td nowrap align="right">
				        <fmt:message key="setup.mail.password" />
				    </td>
				    <td>
				        <input type="password" name="password" value="<%= ((password != null) ? password : "") %>"  id="loc01" /> 
				    </td>
					<td>
						<span class="jawa-setup-helpicon" onmouseover="domTT_activate(this, event, 'content', '<fmt:message key="setup.mail.password" />', 'styleClass', 'jawaTooltip', 'trail', true, 'delay', 300, 'lifetime', 8000);"></span>
							        <%  if (errors.get("password") != null) { %>
							            <span class="jawa-error-text">
							            <%= errors.get("password") %>
							            </span>
							        <%  } %>
					</td>
				</tr>
			</table>

				<br />

				<div align="right">
					<input type="submit" name="continue" value="<fmt:message key="button.continue" />" id="jawa-setup-save" border="0">
				</div>
			</form>

	</div>
	<!-- END jawa-contentBox -->
</body>
</html>
